package netty.zerocopy;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * Created with IntelliJ IDEA.
 * User: SuQiEr
 * Date:2017/11/4 0004
 * Time: 下午 4:52
 * To change this template use File | Settings | File Templates.
 */
public class NewIOClient {

    public static void main(String[] args) throws IOException {

        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("localhost",8080));

        String filePath = "/data/file_test.mp4";

        long startTime = System.currentTimeMillis();
        FileChannel fileChannel = new FileInputStream(filePath).getChannel();
        System.out.println("开始发送:");
        /**operating systems can transfer bytes directly from the filesystem cache
         *to the target channel without actually copying them
         * */
        fileChannel.transferTo(0,fileChannel.size(),socketChannel);
        System.out.println("耗时:"+(System.currentTimeMillis()-startTime));

    }
}
